import { AxiosRequestConfig } from './types';
import { processHeaders } from './helpers/header';
import { transformRequest, transformResponse } from './helpers/data';

const defaults: AxiosRequestConfig = {
  method: 'get',
  timeout: 0,
  headers: {
    common: {
      Accept: 'application/json, text/plain, */*',
    },
  },
  xsrfCookieName: 'XSRF-TOKEN',
  xsrfHeaderName: 'XSRF-TOKEN',
  transformRequest: [
    function(data: any, headers: any) {
      processHeaders(headers, data);
      return transformRequest(data);
    },
  ],
  transformResponse: [
    function(data: any) {
      return transformResponse(data);
    },
  ],
  validateStatus: (status: number) => {
    return status >= 200 && status < 300;
  },
};

const methodWithoutData = ['get', 'delete', 'head', 'options'];
const methodWithData = ['post', 'put', 'patch'];

methodWithoutData.forEach(method => {
  defaults['headers'][method] = {};
});

methodWithData.forEach(method => {
  defaults['headers'][method] = {
    'Content-Type': 'application/x-www-form-urlencoded',
  };
});

export default defaults;
